package com.app.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.common.TotalConfig;
import com.app.model.SysUser;
import com.app.pojo.PaymentQuotaConfig;
import com.app.service.IPaymentQuotaConfigService;
import com.app.service.IPaymentRecordsLogService;
import com.app.service.ISysUserService;
import com.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/adminPaymentQuota")
@Api(tags = "admin缴费接口")
public class AdminPaymentQuotaConfigController {
    @Autowired
    private IPaymentQuotaConfigService paymentQuotaConfigService;
    @Autowired
    private IPaymentRecordsLogService paymentRecordsLogService;
    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "更改缴费额度")
    @PostMapping("/setPaymentQuota")
    public Result<Object> setPaymentQuota(@RequestBody PaymentQuotaConfig paymentQuotaConfig) {
        if (!StpUtil.hasPermission("admin:payment:amount:list")){
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        if (paymentQuotaConfig == null) {
            return Result.error("新增修改的数据不能为空");
        }
        if (paymentQuotaConfig.getId() == null) {//新增
            SysUser sysUser = sysUserService.getById(StpUtil.getLoginIdAsLong());
            paymentQuotaConfig.setCreateUser(sysUser.getId());
            paymentQuotaConfig.setCreateTime(LocalDateTime.now());
            if (paymentQuotaConfig.getSort() == null) {
                paymentQuotaConfig.setSort(0);
            }
            paymentQuotaConfigService.save(paymentQuotaConfig);
            return Result.success("新增成功");
        }else{//修改
            SysUser sysUser = sysUserService.getById(StpUtil.getLoginIdAsLong());
            paymentQuotaConfig.setUpdateUser(sysUser.getId());
            paymentQuotaConfig.setUpdateTime(LocalDateTime.now());
            paymentQuotaConfigService.updateById(paymentQuotaConfig);
            return Result.success("修改成功！");
        }
    }



    @ApiOperation(value = "缴费额度的列表查询")
    @GetMapping("/list")
    public Result<List<PaymentQuotaConfig>> list(Integer page, Integer limit ) {
        return paymentQuotaConfigService.quotaList(page,limit);
    }
    /**
     * admin查询缴费记录
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询缴费记录")
    @GetMapping("/paymentRecord")
    public Result<Object> getPaymentRecord(Integer page, Integer limit,String keyword,Integer state) {
        if (!StpUtil.hasPermission("admin:payment:record:list")){
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        return paymentRecordsLogService.paymentRecord(page,limit,keyword,state);
    }

    @ApiOperation(value = "支付记录详情")
    @GetMapping("/paymentLogDetail")
    public Result<Object> paymentLogDetail(Integer id){
        return Result.success(paymentRecordsLogService.getById(id));
    }
}
